import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { Button, Spin, Form, Input, Table,  Modal, Select,message } from 'antd'
import moment from 'moment';
/* 引用接口文档路径 */
import {
  classroomSelect,classroomInfo,addClassroom,deleteClassroom
} from 'actions/materialManage'
import '../../style/userControl.less'
import ModalAddClassroom from './addClassroomModal.js';

const FormItem = Form.Item
@Form.create({})
//abc
@connect(
  (state, props) => ({
    config: state.config,
    userInfoList: state.userInfoList,
  })
)
export default class app extends Component {
  constructor(props) {
    super(props)
    this.state = {
      list:"",//基础数据列表
      contentModal:false,//列表内容详情弹窗
      modalList:"",//列表内容详情
      addModalFlg:false,//新增课堂弹窗
      addModalType:"",
    }
    this.keyword={"title":"","publisherName":"",};//搜索关键词
    this.currentNumber = 1;//当前页面
    this.pagesize = 10;//每页条数
    this.typeId = "0";//课堂类型
  }
  componentDidMount() {
    this.selectList();
  }
  
  selectFn(){//点击搜索按钮
    this.keyword = {
      "title":this.props.form.getFieldValue('title'),
      "publisherName":this.props.form.getFieldValue('publisherName'),
    };
    this.currentNumber=1;
    this.selectList();
  }
  selectList(pageNumber){//查询列表
    const datajson = Object.assign({"start":pageNumber||1,"end":this.pagesize}, this.keyword);
    this.currentNumber = pageNumber||1;
    this.props.dispatch(classroomSelect(
      datajson,
      (respose) => {
        this.setState({
          list: respose,
        })
    }))
  }
  
  changeSelect(value){//课堂类型
    this.typeId = value;
  }

  addReport(type,id){//新增课堂
    //编辑的情况  带数据去组件
    if(type == 2){
      this.props.dispatch(classroomInfo({"id":id},
      (respose) => {
          this.setState({
          addModalFlg: true,
          modalList:respose,
          addModalType:type,
        })
      }))
    }else{//新增的情况
      this.setState({
        addModalFlg: true,
        modalList:{},
        addModalType:type,
      })
    }

  }

  addModalCancel(){//关闭新增课堂弹窗
    this.setState({
      addModalFlg: false,
    })
  }



  contentModalCancel(){//关闭查看弹窗
    this.setState({
      contentModal: false,
    },()=>{
      this.setState({
        modalList: "",
      })
    })
  }

  showModal(id){//点击查看  大咖课堂详情
    this.props.dispatch(classroomInfo({"id":id},
      (respose) => {
        this.setState({
          contentModal: true,
          modalList:respose
        })

    }))
  }

  deleteList(id){
    Modal.confirm({
      content:"确定要删除该条信息么？",
      onOk:()=>{
        this.props.dispatch(deleteClassroom(
          {"id":id},
          (respose) => {
            var length = this.state.list.respData.count ? this.state.list.respData.count : 0;
            if(Math.ceil(length/this.pagesize) == this.currentNumber && length%this.pagesize == 1 && this.currentNumber != 1){
              this.currentNumber = this.currentNumber - 1;
            }
            message.success(respose.respMsg);
            this.contentModalCancel();
            this.selectList(this.currentNumber);
        }))
      },
      onCancel:()=>{

      }
    })
  }



  columns() {//表格标题
    return [
      {
        title: '课堂编号',
        key: 'code',
        dataIndex: 'code',
        width: '10%',
      },
      {
        title: '课堂标题',
        dataIndex: 'title',
        key: 'title',
        width: '10%',
      },
      {
        title: '课堂封面',
        dataIndex: 'imageAddr',
        key: 'imageAddr',
        width: '10%',
        render: (text, recordId, index) => <img style={{width:'50px'}} src={text ? text : require('../../images/baseImg.png')} />,
      },
      {
        title: '发布人',
        dataIndex: 'publisherName',
        key: 'publisherName',
        width: '10%',
      },
      {
        title: '发布时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: '10%',
      },
      {
        title: '阅读数',
        dataIndex: 'readTotal',
        key: 'readTotal',
        width: '10%',
      },
      {
        title: '评论数',
        dataIndex: 'commentTotal',
        key: 'commentTotal',
        width: '10%',
      },
      {
        title: '点赞数',
        dataIndex: 'fabulousTotal',
        key: 'fabulousTotal',
        width: '10%',
      },
      {
        title: '状态',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          {
            (()=>{
              switch(text.dataStatus){
                case 3 :
                return '已发布'
                case 5 :
                return '已保存'
              }
            })()
          }
        </div>,
        
      },
      {
        title: '操作',
        width: '10%',
        render: (text, recordId, index) => 
        <div>
          <Button type="primary" size="small" onClick={this.showModal.bind(this,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>查看</Button>          
          <Button type="primary" size="small" onClick={this.addReport.bind(this,2,text.id)} style={{marginRight:"5px",marginBottom:"5px"}}>编辑</Button>  
        </div>,
        
      },
    ]
  }
  


  render () {
    const {form } = this.props
    const { getFieldDecorator } = form    
    const respData = this.state.list.respData ? this.state.list.respData : [];//刷新页面调用接口数据赋值
    const modalList = this.state.modalList.respData ? this.state.modalList.respData :[];
    const dateFormat = 'YYYY/MM/DD';
    return (
      <div className="page">
        {/* 搜索条件筛选块 */}
        <div className="searcDiv">
          <Form onSubmit={this.selectFn.bind(this)} layout="inline">
            <FormItem label="课堂标题">
              {
                getFieldDecorator('title', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <FormItem label="发布人">
              {
                getFieldDecorator('publisherName', {
                  rules: [{
                    required: false,
                  }],
                })(
                  <Input placeholder="请输入关键字" size="default" style={{ width: '200px' }} />
                )
              }  
            </FormItem>
            <Button type="primary" onClick={this.selectFn.bind(this)}>搜索</Button>
            <Button type="primary" onClick={this.addReport.bind(this,1)}>新增</Button>
          </Form>
        </div>
        {/*数据渲染列表*/}
        <Spin spinning={false}>
          <Table
            rowKey="id" 
            columns={this.columns()}//表格标题列
            dataSource={respData.resultData}
            pagination={{ total: respData.count, current:this.currentNumber,pageSize: this.pagesize, onChange: this.selectList.bind(this)}}
            scroll={{ y: true }}
          />
        </Spin>
        {/* 新增课堂弹窗 */}
        {
          this.state.addModalFlg ?
          <ModalAddClassroom 
            type={this.state.addModalType}
            ModalFlg={this.state.addModalFlg} 
            ModalCancel={this.addModalCancel.bind(this)} 
            callback={this.selectList.bind(this,this.state.addModalType==1?1:this.currentNumber)}
            content={this.state.modalList.respData ? this.state.modalList.respData : {}}
          /> :""
        }
        {/* 信息展示弹窗 */}
        <Modal
          width={"80%"}
          visible={this.state.contentModal}
          title="课堂详情"
          onCancel={this.contentModalCancel.bind(this)}
          footer={[
            <Button key="submit" type="primary"  onClick={this.deleteList.bind(this,modalList.id)}>删除</Button>,
          ]}
        >
          <ul className="joinBlackListModal">
            <li>
              <span className="title">发布人:</span><span className="content">{modalList.publisherName + modalList.positionName}</span>
            </li>
            <li>
              <span className="title">发布时间:</span><span className="content">{modalList.createTime}</span>
            </li>
            <li>
              <span className="title">课堂标题:</span><span className="content">{modalList.title}</span>
            </li>
            <li>
              <span className="title">课堂封面:</span><span className="content"><img style={{width:"80px"}} src={modalList.imageAddr?modalList.imageAddr:require('../../images/baseImg.png')}/></span>
            </li>
            <li>
              <span className="title">课堂介绍:</span><span className="content" dangerouslySetInnerHTML={{__html: modalList.detail}}></span>
            </li>
            <li>
              <span className="title">大咖介绍:</span><span className="content">{modalList.introduce}</span>
            </li>
            <li>
              <span className="title">累计评论:</span><span className="content">{modalList.commentTotal}</span>
            </li>
            <li>
              <span className="title">累计被赞:</span><span className="content">{modalList.fabulousTotal}</span>
            </li>
            <li>
              <span className="title">累计阅读:</span><span className="content">{modalList.readTotal}</span>
            </li>
          </ul>
        </Modal>
      </div>
    )
  }
}
